package com.huatai.bpmn.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.bpmn.commom.util.WechatUtil;
import com.huatai.bpmn.entity.DepartmentEntity;
import com.huatai.bpmn.entity.DepartmentUserEntity;
import com.huatai.bpmn.entity.UserEntity;
import com.huatai.bpmn.entity.dto.WeChatDeptDto;
import com.huatai.bpmn.mapper.DepartmentMapper;
import com.huatai.bpmn.service.DepartmentService;
import com.huatai.bpmn.service.DepartmentUserService;
import com.huatai.bpmn.service.UserService;
import com.huatai.common.util.ObjectUtil;
import com.huatai.user.dto.WeChatPeopleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author yxy
 * @since 2023-09-20
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, DepartmentEntity> implements DepartmentService {

	@Value("${wechat.corpid}")
	private String corpid;
	@Value("${wechat.corpsecret}")
	private String corpsecret;

	@Autowired
	private StringRedisTemplate redisTemplate;

	@Autowired
	private DepartmentUserService departmentUserService;
	@Autowired
	private UserService userService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean pullData() {

		//获取部门列表
		List<WeChatDeptDto> deptList = WechatUtil.getDeptList();
		deptList.forEach(x -> {
			List<WeChatPeopleDto> deptPeopleList = WechatUtil.getDeptPeopleList(x.getId(), x.getName());
			String departName = x.getName();
			DepartmentEntity departmentEntity = this.getOne(new LambdaQueryWrapper<>(DepartmentEntity.class).eq(DepartmentEntity::getDeptName, departName));
			//同步部门信息
			if (ObjectUtils.isEmpty(departmentEntity)) {
				String leader = x.getDepartmentLeader();
				departmentEntity = new DepartmentEntity();
				departmentEntity.setSort(Integer.valueOf(x.getOrder()));
				departmentEntity.setDeptName(x.getName());
				departmentEntity.setLeader(leader.substring(1, leader.length() - 1));
				departmentEntity.setParentId(Long.valueOf(x.getParentid()));
				this.saveOrUpdate(departmentEntity);
			}
			ArrayList<DepartmentUserEntity> departmentUserList = new ArrayList<>();
			//同步部门用户
			deptPeopleList.forEach(y -> {
				String name = y.getName();
				String pyName = PinyinUtil.getPinyin(name).replaceAll(" ", "");
				UserEntity user1 = userService.getOne(new LambdaQueryWrapper<>(UserEntity.class).eq(UserEntity::getUserAccount, pyName));
				if (!ObjectUtil.isEmpty(user1)) {
					List<DepartmentUserEntity> list = departmentUserService.list(new LambdaQueryWrapper<>(DepartmentUserEntity.class).eq(DepartmentUserEntity::getUserId, user1.getUserId()).eq(DepartmentUserEntity::getDepartmentId, x.getId()));
					if (list.size()==0){
						DepartmentUserEntity departmentUserEntity = new DepartmentUserEntity();
						departmentUserEntity.setUserId(user1.getId());
						departmentUserEntity.setDepartmentId(Long.valueOf(x.getId()));
						departmentUserList.add(departmentUserEntity);
					}
				}

			});
			departmentUserService.saveOrUpdateBatch(departmentUserList);
		});
		return true;
	}

	@Override
	public List<UserEntity> getUserByDepartId(String departId) {
		List<DepartmentUserEntity> list = departmentUserService.list(new LambdaQueryWrapper<>(DepartmentUserEntity.class).eq(DepartmentUserEntity::getDepartmentId, departId));
		ArrayList userList = new ArrayList<UserEntity>();
		list.forEach(x -> {
			UserEntity user = userService.getById(x.getUserId());
			userList.add(user);
		});
		return userList;
	}
}
